var elements = {
    people: new Array(),
    orderCells: new Array(),
    peopleData: null,
    load: function() {
        people.total = document.getElementById("numPeople").value;

        for (var i = 0; i < people.total; i++) {
            this.people[i] = document.getElementById("person" + i);
            this.orderCells[i] = document.getElementById("order" + i);
            people.ordering[i] = null;
            people.ids[i] = null;
        }

        this.peopleData = document.getElementById("hidden-players");

        gameStyle.element = document.getElementById("gameStyleElem");
        click(gameStyle);

        scoreLimit.element = document.getElementById("scoreLimitElem");
        click(scoreLimit);

        forceAllThrows.element = document.getElementById("forceAllThrowsElem");
        click(forceAllThrows);

        endingStyle.element = document.getElementById("endingStyleElem");
        click(endingStyle);

        penalties.element = document.getElementById("penaltiesElem");
        penalties.harshScaleRow = document.getElementById("harshScaleRow");
        penalties.harshScaleElem = document.getElementById("harshScaleElem");
        click(penalties);
    }
};

var people = {
    total: 0,
    players: 0,
    ordering: new Array(),
    ids: new Array(),

    deselect: function(index) {
        for (var i = 0; i < this.total; i++)
            if (this.ordering[i] > this.ordering[index])
                this.ordering[i] --;
        this.ordering[index] = null;
        this.players --;
        elements.people[index].style.cssText = "";
    },

    select: function(index, style) {
        this.ordering[index] = this.players;
        this.players ++;
        elements.people[index].style.cssText = style;
    },

    click: function(index, personId, style) {
        this.ids[index] = personId;

        if (this.ordering[index] == null)
            this.select(index, style);
        else
            this.deselect(index);

        for (var i = 0; i < this.total; i++)
            elements.orderCells[i].innerHTML = (this.ordering[i] != null ? this.ordering[i] + 1 : "");
    },

    populate: function() {
        var data = new Array(this.players);
        for (var i = 0; i < this.total; i ++) {
            var order = this.ordering[i];
            if (order != null)
                data[order] = this.ids[i];
        }


        elements.peopleData.innerHTML = "";
        for (var j = 0; j < this.players; j ++)
            elements.peopleData.innerHTML += "<input type='hidden' name='playerGames["+j+"].person.id' value='"+data[j]+"'> ";
    }
};

var gameStyle = {
    element: null,
    state: -1,
    output: ['Count Down' + hidden('countDown', 'true'),
        'Count Up' + hidden('countDown', 'false')]
};

var scoreLimit = {
    element: null,
    state: -1,
    output: ['None' + hidden('scoreLimit', ''),
        text('scoreLimit', '0')],
    validate: function() {
        if (this.state == 0)
            return true;

        var value = document.getElementById('scoreLimit').value;
        var test = (value != "");
        test &= !value.match(/\s+/);
        return Boolean(test);
    }
};

var forceAllThrows = {
    element: null,
    state: -1,
    output: ['Yes' + hidden('forceAllThrows', 'true'),
        'No' + hidden('forceAllThrows', 'false')]
};

var endingStyle = {
    element: null,
    state: 1,
    output: ['Bust' + hidden('bounceBack', ''),
        'No Bounce Back' + hidden('bounceBack', 'off'),
        text('bounceBack', '0')],
    validate: function() {
        if (this.state == 0 || this.state == 1)
            return true;

        var value = document.getElementById('bounceBack').value;
        var test = (value != "");
        test &= !value.match(/\s+/);
        return Boolean(test);
    }
};

var penalties = {
    element: null,
    harshScaleRow: null,
    harshScaleElem: null,
    state: 2,
    output: ['No Penalties' + hidden('harshAndMainPenalties', 'false') + hidden('harshScale', '0'),
        'Main only' + hidden('harshAndMainPenalties', 'true') + hidden('harshScale', '0'),
        'Harsh only' + hidden('harshAndMainPenalties', 'false'),
        'Main and Harsh' + hidden('harshAndMainPenalties', 'true')],
    more: function() {
        switch (this.state) {
        case 0:
            this.harshScaleRow.style.visibility = "hidden";
            this.harshScaleElem.innerHTML = "";
            return;
        case 2:
            this.harshScaleRow.style.visibility = "visible";
            this.harshScaleElem.innerHTML = text('harshScale', '0.05');
        }
    },
    validate: function() {
        if (this.state == 0 || this.state == 1)
            return true;

        var value = document.getElementById('harshScale').value;
        var test = (value != "");
        test &= !value.match(/\s+/);
        return Boolean(test);
    }
};

function click(object) {
    object.state = (object.state + 1) % object.output.length;
    object.element.innerHTML = object.output[object.state];
    if (object.more) {
        object.more();
    }
}

function hidden(name, value) {
    return "<input type='hidden' id'" + name + "' name='" + name + "' value='" + value + "' />";
}

function text(name, value) {
    return "<input type='text' id='" + name + "' name='" + name + "' value='" + value + "' />";
}

function trySubmit() {
    try {
        if (people.players <= 0)
            return false;

        var test = scoreLimit.validate();
        test &= endingStyle.validate();
        test &= penalties.validate();

        if (!test)
            return false;

        people.populate();
        return true;
    } catch(e) {
        alert(e);
        return false;
    }
}